package controller;

import java.security.InvalidParameterException;

import model.Customer;
import model.CustomerCollection;
import model.Item;
import model.ItemCollection;
import model.NoSuchItemException;
import model.Sale;
import model.SaleCollection;
import model.Delivery;
import model.SaleLineItem;

/**
 *
 * @author Mads Nielsen
 */
public class SaleController {

    private Sale currentSale;
    private ItemCollection items;
    private SaleCollection sales;

    public SaleController() {
        items = ItemCollection.getInstance();
        sales = SaleCollection.getInstance();

        startSale();
    }

    public static void loadData() {
        Sale s1 = new Sale();

        Item item1 = ItemCollection.getInstance().getItem("1");
        s1.addItem(item1, 1);

        Item item2 = ItemCollection.getInstance().getItem("2");
        s1.addItem(item2, 1);

        Customer customer = CustomerCollection.getInstance().getCustomer(
                12345678);

        s1.setCustomer(customer);

        Delivery delivery = new Delivery(s1);
        delivery.setName("Bar");
        delivery.setAddress("foovej 1");
        delivery.setZipCode(1234);
        delivery.setCity("Barville");
        delivery.setPrice(88.40);
        
        s1.close();

        SaleCollection.getInstance().addSale(s1);
    }

    public void startSale() {
        currentSale = new Sale();
    }

    /**
     * Adds item to current sale in specified quantity
     *
     * @param itemNo
     * @param qty
     * @throws NoSuchItemException
     *             if item identifier does not match any known item
     * @throws NoOpenSaleException
     *             if no sale is currently open
     */
    public void enterItem(String itemNo, int qty) throws NoSuchItemException,
            NoOpenSaleException {

        if (currentSale == null) {
            throw new NoOpenSaleException();
        }

        if (0 > qty) {
            throw new InvalidParameterException(
                    "Quantity must be larger than zero");
        }

        Item item = items.getItem(itemNo);
        if (item == null) {
            throw new NoSuchItemException();
        }
        currentSale.addItem(item, qty);
    }

    public String getItemName(String itemNo) {
        Item item = items.getItem(itemNo);
        if (item == null) {
            return "";
        } else {
            return item.getName();
        }

    }

    public void registerCashPayment(double amount) {
        currentSale.registerCashPayment(amount);
    }

    public void closeSale() {
        currentSale.close();
        sales.addSale(currentSale);
    }

    public void cancelSale() {
        currentSale.cancelSale();
    }

    public boolean useCustomerAccount(int phone) {
        // search for customer
        CustomerController controller = new CustomerController();
        Customer customer = controller.getCustomer(phone);
        if (customer == null) {
            return false;
        }

        // use in sale
        currentSale.setCustomer(customer);
        return true;
    }

    public void unsetCustomer() {
        currentSale.setCustomer(null);
    }

    public void createDelivery(String name, String address, int zipCode,
            String city, double price) {
        DeliveryController deliveryController = new DeliveryController();
        deliveryController.createDelivery(currentSale, name, address, zipCode, city, price);
    }

    public Sale[] getAllSales() {
        return sales.getAllSales();
    }

    public Sale getSale() {
        return currentSale;
    }

    public SaleLineItem getSaleLine(int index) {
        return currentSale.getLine(index);
    }

    public void removeSaleLine(SaleLineItem line) {
        currentSale.removeLine(line);
    }

    public SaleLineItem[] getSaleLineItems() {
        return currentSale.getLineItems();
    }

    public void setDiscount(double discount) {
        currentSale.setDiscount(discount);
    }

    public double getSaleSavings() {
        return currentSale.getSavings();
    }

    public void clearSaleDelivery() {
        currentSale.clearDelivery();
    }
}
